package com.naiterui.ehp.bs.b2c.product.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.b2c.product.exception.ProductBusinessException;
import com.naiterui.ehp.bs.b2c.product.exception.ProductExceptionCodes;
import com.naiterui.ehp.bs.b2c.product.service.IProductService;
import com.naiterui.ehp.bs.b2c.product.vo.ProductDetailVO;
import com.naiterui.ehp.bs.b2c.product.vo.ProductInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <pre>
 * 商品相关
 * </pre>
 *
 * @author fangguanhong
 * @date 2019/5/18 上午10:34
 * @since 1.0.0
 */
@Api(tags = SwaggerTag.DOCTOR + SwaggerTag.MEDICINE_DRUG)
@RequestMapping("b2c/product")
@RestController
public class ProductController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private IProductService productService;

    /**
     * 分类/关键字搜索商品列表
     *
     * @param categoryId
     * @param keyword
     *
     * @return
     */
    @ApiOperation(value = "分类/关键字搜索商品列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "categoryId", value = "分类ID", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "keyword", value = "关键词", paramType = "query", dataType = "string")})
    @GetMapping("/list")
    public ResponseVO<PageVO<ProductInfoVO>> list(PageParamVO pageParamVO, Integer categoryId, String keyword) throws ProductBusinessException {
        LOGGER.info("分类/关键字搜索商品列表，参数为 categoryId {}, keyword {}", categoryId, keyword);
        /*if (null == categoryId && StringUtils.isEmpty(keyword)) {
            throw new ProductBusinessException(ProductExceptionCodes.PARAM_ERROR);
        }*/
        PageVO<ProductInfoVO> result = this.productService.getProductList(pageParamVO, categoryId, keyword);
        return PublicService.returnResponseVO(ProductExceptionCodes.SUCCESS, result);
    }

    /**
     * 商品详情
     *
     * @param skuId
     *
     * @return
     */
    @ApiOperation(value = "商品详情")
    @ApiImplicitParam(name = "skuId", value = "skuId", paramType = "query", dataType = "long", required = true)
    @GetMapping("/detail")
    public ResponseVO<ProductDetailVO> detail(Long skuId) throws ProductBusinessException {
        LOGGER.info("获取商品详情信息，参数为：skuId {}", skuId);
        if (null == skuId) {
            throw new ProductBusinessException(ProductExceptionCodes.PARAM_ERROR);
        }
        ProductDetailVO result = this.productService.getProductDetail(skuId);
        return PublicService.returnResponseVO(ProductExceptionCodes.SUCCESS, result);
    }

}
